<?php


// DATAFEEDR IMPORT
	$stop_shop = false;	$shop_added=0;	$shop_updated=0;	$cat_added=0; $shop_deleted = 0; $DatabaseCheck=0;
	
	$search = array("\n", "\r");
	$replace = array("","");		
	$AddtoCats = array();																							

	$open_file = fopen($_POST['filename'], "r");
	
	if (!$open_file) {die('<div class="error" id="message" style="margin-top: 10px;"><p>File can not be read or found</p></div>');}
	
	stream_set_timeout($open_file, 3000); $count=0;	
		
	// Loop through each line
	while (!feof ($open_file)) {$lines = fgets($open_file);
	if (isset($lines)) {
	$count++;	
	// store each attribute as an array
	$arr = explode("|",$lines);if (count($arr) > 8) {
 

//die(print_r($arr));
			
				$id 			= $wpdb->escape($arr[0]);
				$name 			= $wpdb->escape($arr[1]);
				$link 			= $wpdb->escape($arr[2]);
				$description 	= ($arr[9]==2) ? '' : $wpdb->escape($arr[3]);
				$price 			= $wpdb->escape($arr[4]);
				$merchant 		= $wpdb->escape($arr[5]);
				$category 		= $wpdb->escape($arr[6]);
				$image 			= $wpdb->escape($arr[7]);
				$thumbnail 		= $wpdb->escape($arr[8]);
				$network 		= $wpdb->escape($arr[9]);
				$visible 		= 1;
				$img_info 		= "";
				$tagsString = "";
				$tags1 = str_replace(" ",",",str_replace("-","",$name));
				$tagss = explode(",",$tags1);
				foreach($tagss as $thistag){
					if(strlen($thistag) > 3){
						$tagsString .= $thistag.",";
					}
				}
 

				$data['nicename'] = str_replace(" ","-",str_replace("com","",$name));
				$data['nicename'] = str_replace("--","-",$data['nicename']);
				$data['nicename'] = str_replace("/","-",$data['nicename']);
				$data['nicename'] = str_replace("--","-",$data['nicename']);
				$data['nicename'] = str_replace(".","",$data['nicename']);
				$data['nicename'] = str_replace("'","",$data['nicename']);
				$data['nicename'] = str_replace(",","",$data['nicename']);
				$data['nicename'] = str_replace(")","",$data['nicename']);
				$data['nicename'] = str_replace("(","",$data['nicename']);
				$data['nicename'] = str_replace(",","",$data['nicename']);
				$data['nicename'] = str_replace("!","",$data['nicename']);
				$data['nicename'] = str_replace(":","",$data['nicename']);

if($DatabaseCheck ==1){
				// CHECK THIS PRODUCT DOESNT ALREADY EXIST		
				$result = mysql_query("SELECT $wpdb->posts.ID AS total FROM $wpdb->posts
				INNER JOIN $wpdb->postmeta ON
				($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key ='affiliate_productID' AND $wpdb->postmeta.meta_value='".$id."') 
				LIMIT 1", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__);
						
				$array = mysql_fetch_assoc($result);
}
					//print $category."<-- <br> "; 
				if (empty($array['total']) || $DatabaseCheck=0) {

				// save the product images locally
				if($_POST['store_image'] =="yes"){	
					$saveName = SaveImage($image);
					if($saveName !=""){
						$image = $saveName;
					}	
					$saveName1 = SaveImage($thumbnail);
					if($saveName1 !=""){
						$thumbnail = $saveName1;
					}
				}

				$cats_array = explode("_",$category);
				foreach($cats_array as $Addcat){					  
					
				$v = strip_tags(trim($Addcat));	
				$catzID = wp_create_category1($v, 0);
 

				}

				// CREATE THE PRODUCT
				$my_post = array();
				$my_post['post_title'] 		= $name;
				$my_post['post_content'] 	= $description;
				$my_post['post_excerpt'] 	= $description;
				$my_post['post_author'] 	= 1;
				$my_post['post_status'] 	= "publish";				
				$my_post['tags_input'] 		= $tagsString;				
				$my_post['post_category'] 	= array($catzID);	
				$POSTID = wp_insert_post( $my_post );	
  
				update_post_meta($POSTID,"image",$image);
				update_post_meta($POSTID,"merchant",$merchant);
				update_post_meta($POSTID,"price",$price);
				update_post_meta($POSTID,"network",$network);
				update_post_meta($POSTID,"thumbnail",$thumbnail);
				update_post_meta($POSTID,"link",$link);
				update_post_meta($POSTID,"affiliate_productID",$id);

				$shop_added++;
 
				}else{

				// UPDATE THE PRODUCT
				$my_post = array();			
				$my_post['ID'] 				= $array['total'];
				$my_post['post_title'] 		= $name;
				$my_post['post_content'] 	= $description;
				$my_post['post_excerpt'] 	= $description;
				$my_post['post_author'] 	= 1;
				$my_post['post_status'] 	= "publish";				
				$my_post['tags_input'] 		= $tagsString;				
				$my_post['post_category'] 	= $AddtoCats;	
				wp_update_post( $my_post );	
  
				update_post_meta($array['total'],"image",$image);
				update_post_meta($array['total'],"merchant",$merchant);
				update_post_meta($array['total'],"price",$price);
				update_post_meta($array['total'],"network",$network);
				update_post_meta($array['total'],"thumbnail",$thumbnail);
				update_post_meta($array['total'],"link",$link);
				update_post_meta($array['total'],"affiliate_productID",$id);

				$shop_updated++;
 

				}
 
			// if there are less than 8 values in this array, it must be a category
			} else {
			
				$arr[1] = isset($arr[1] ) ? $arr[1] : '';
				$arr[2] = isset($arr[2] ) ? $arr[2] : 0;
				
				$id 	= $wpdb->escape(str_replace($search,$replace,$arr[0]));
				$name   = $wpdb->escape($arr[1]);
				$parent = $wpdb->escape(str_replace($search,$replace,$arr[2]));	
			
				wp_create_category1($name, 0);			 
			
			}			
		}
	}
	
	$stop_shop=true;
	
	if ($stop_shop) {
	
		$GLOBALS['errorMsg'] .= "Your file been sucessfully imported.<br /><br />
		<b>Products Added:</b> " . $shop_added . "<br />
		<b>Previous Products Updated:</b> " . $shop_updated . "<br /></div>";
	 
	}  
 

?>